<?php
/**
 * @desc:
 * wsxc - BaseWsxcService.php
 * @author: quanjianyu
 * @email: 164174421@qq.com
 * @dateTime：2021/4/7 4:11
 */

namespace wsxc\api;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Uri;
use wsxc\api\impl\ShopGoodsServiceImpl;
use wsxc\api\impl\ShopServiceImpl;
use wsxc\bean\BaseResult;
use wsxc\config\IConfig;

abstract class AbstractBaseWsxcService implements IWsxcService {

    protected IConfig $config;
    protected Client $http;
    private IShopService $shopService;
    private IShopGoodsService $goodsService;

    /**
     * AbstractBaseWsxcService constructor.
     */
    public function __construct() {
        $this->shopService = new ShopServiceImpl($this);
        $this->goodsService = new ShopGoodsServiceImpl($this);
    }


    /**
     * @param IConfig $config
     */
    public function setConfig(IConfig $config): void {
        $this->config = $config;
        $this->initHttp();
    }

    public function getConfig(): IConfig {
        return $this->config;
    }

    public function initHttp(): void {
        $this->http = new Client($this->config->toArray());
    }

    /**
     * @desc: get请求
     * @param string $var1
     * @param array $var2
     * @param \stdClass $stdClass
     * @return array
     * @throws GuzzleException
     * @throws \JsonMapper_Exception
     * @author: quanjianyu
     * @email: 164174421@qq.com
     * @dateTime：2021/4/10 0:23
     */
    public function get(string $var1, array $var2, string $stdClass): BaseResult {
        try {
            $uri = new Uri();
            $url = $uri->withPath($var1);
            $response = $this->http->request('get', $url, [
                'query' => $var2
            ]);
            $statusCode = $response->getStatusCode();
            if ($statusCode != 200) throw new \Exception('请求出错');
            $res = json_decode($response->getBody());
            $jsonMapper = new \JsonMapper();
            $baseResult = $jsonMapper->map($res, new $stdClass());
            if ($baseResult->getErrcode() > 0) throw new \Exception($baseResult->getErrmsg());
            return $baseResult;
        } catch (GuzzleException $guzzleException) {
            throw $guzzleException;
        } catch (\Exception $exception) {
            throw $exception;
        }
    }

    /**
     * @desc: post请求
     * @param string $var1
     * @param array $getParams
     * @param array $postParams
     * @param string $stdClass
     * @return BaseResult
     * @throws GuzzleException
     * @throws \JsonMapper_Exception
     * @author: quanjianyu
     * @email: 164174421@qq.com
     * @dateTime：2021/4/10 0:23
     */
    public function post(string $var1, array $getParams, array $postParams, string $stdClass): BaseResult {
        $uri = new Uri();
        $url = $uri->withPath($var1);
        $response = $this->http->request('post', $url, [
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
            ],
            'query' => $getParams,
            'form_params'  => $postParams,
        ]);
        $statusCode = $response->getStatusCode();
        if ($statusCode != 200) throw new \Exception('请求出错');
        $res = json_decode($response->getBody());
        $jsonMapper = new \JsonMapper();
        $baseResult = $jsonMapper->map($res, new $stdClass());
        if ($baseResult->getErrcode() > 0) throw new \Exception($baseResult->getErrmsg());
        return $baseResult;
    }

    public function getShopService(): IShopService {
        return $this->shopService;
    }

    public function getGoodsService(): IShopGoodsService {
        return $this->goodsService;
    }


}